package com.mmall.concurrency.example.singleton;

import lombok.extern.slf4j.Slf4j;

/**
 * 懒汉模式 运行的时候才被创建
 */
@Slf4j
public class SingletonExample5 {
    private SingletonExample5(){
    }

    /**
     * 1.memory=allocate() 分配对象的内存空间
     * 2。ctorInstance() 初始化对象
     * 3. instance =memory 设置instance指向刚分配的内存
     */
    //单利对象 volatile +双重检测机制->禁止重排
    private volatile static SingletonExample5 instance=null;

    public static SingletonExample5 getInstance(){
        if(instance==null){//双重检测
            synchronized(SingletonExample5.class){
                if(instance==null){
                     instance=new SingletonExample5();

                }
            }
        }
        return instance;
    }
}
